package com.ruoyi.weather.service.impl;

import com.ruoyi.common.constant.QyiConstants;
import com.ruoyi.weather.service.IWeatherService;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Service
public class WeatherServiceImpl implements IWeatherService {
    @Override
    public Map<String, Object> callThirdPartyApi(Map<String, Object> params) {

        Map<String, Object> mapparams = new HashMap<>();
        mapparams.put("location", params.get("param1"));
        mapparams.put("key", params.get("param2"));


            try {
                String httpurl = "https://api.seniverse.com/v3/weather/daily.json?";
                String key = (String) params.get("param1");
                String location = (String) params.get("param2");; // 可以根据需要修改成你想要查询的地区

                URL url = new URL(httpurl + "key=" + key + "&location=" + location);
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)");


                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                StringBuffer response = new StringBuffer();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                mapparams.put("weather", response.toString());

                System.out.println(response.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }

        return mapparams;
    }

    public static void main(String[] args) {
        try {
            String location = "beijing"; // 可以根据需要修改成你想要查询的地区
            String httpurl = "https://api.seniverse.com/v3/weather/daily.json?";
            String key = QyiConstants.XZTQ_API;

            URL url = new URL(httpurl + "key=" + key + "&location=" + location);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            System.out.println(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
